package fjh;


import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

import java.io.IOException;
import java.io.PrintStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Water_Test implements java.io.Serializable{

    static Scanner input=new Scanner(System.in);

    public static ArrayList<Water_Person> list=new ArrayList<Water_Person>();

    public static void start()
    {
        System.out.println("*********欢迎使用*********");
        System.out.println("1、查看全部人员信息");
        System.out.println("2、添加人员信息");
        System.out.println("3、删除人员信息");
        System.out.println("4、查看相关人员信息");
        System.out.println("5、修改相关人员信息");
        System.out.println("6、查看区域内所有人员信息");
        System.out.println("7、退出系统");
        System.out.println("************************\n");
    }

    public static void initData()        //初始化系统
    {

        ObjectInputStream oos=null;
        try
        {
            oos=new ObjectInputStream(new FileInputStream("C:\\Users\\mac\\Desktop\\全部人员.doc"));
            ArrayList<Water_Person> list1;
            try {
                list1 = (ArrayList<Water_Person>)oos.readObject();
                for(Water_Person wp1:list1)
                {
                    String name=new String();
                    name=wp1.getName();
                    String telPhone=new String();
                    telPhone=wp1.getTelPhone();
                    String address=new String();
                    address=wp1.getAddress();
                    String note=new String();
                    note=wp1.getNote();
                    String address_state=new String();
                    address_state=wp1.getAddress_state();
                    list.add(new Water_Person(name,telPhone,address,note,address_state));

                }

                System.out.println("相关数据加载成功！！！");
            } catch (ClassNotFoundException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }catch(IOException e)
        {
            System.err.println("人员信息为空，请添加人员信息");
            // e.printStackTrace();
        }finally
        {
            if(oos!=null)
            {
                try {
                    oos.close();
                } catch (IOException e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }
            }
        }




        File file=new File("C:\\Users\\mac\\Desktop\\全部人员.doc");
        if(!file.exists())
        {
            try {
                file.createNewFile();
                System.out.println("全部人员.doc文件加载成功！！！");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else
        {
            System.out.println("全部人员.doc文件加载成功！！！");
        }

        File file1=new File("C:\\Users\\mac\\Desktop\\全部人员的查看.doc");
        if(!file1.exists())
        {
            try {
                file1.createNewFile();
                System.out.println("全部人员的查看.doc文件加载成功！！！");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else
        {
            System.out.println("全部人员的查看.doc文件加载成功！！！");
        }

    }

    public static void To_View()     //查看全部人员信息
    {
        ObjectInputStream oos=null;
        try
        {
            oos=new ObjectInputStream(new FileInputStream("C:\\Users\\mac\\Desktop\\全部人员.doc"));
            ArrayList<Water_Person> list;
            try {
                list = (ArrayList<Water_Person>)oos.readObject();

                PrintStream out=System.out;
                PrintStream ps=new PrintStream(new FileOutputStream("C:\\Users\\mac\\Desktop\\全部人员的查看.doc"));
                System.setOut(ps);

                System.out.println("姓名\t\t电话\t\t\t家庭住址\t\t\t\t备注\t\t\t\t\t地址所在地");
                for(Water_Person wp1:list)
                {
                    System.out.println(wp1.getName()+"\t"+wp1.getTelPhone()+"\t"+wp1.getAddress()+"\t\t"+wp1.getNote()+"\t\t"+wp1.getAddress_state());

                }
                System.setOut(out);
                System.out.println("人员信息已全部加载至桌面：《全部人员的查看》");
            } catch (ClassNotFoundException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }catch(IOException e)
        {
            System.err.println("人员信息为空，请添加人员信息");
            // e.printStackTrace();
        }finally
        {
            if(oos!=null)
            {
                try {
                    oos.close();
                } catch (IOException e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }
            }
        }
    }

    public static void add_Person()    //添加人员信息
    {
        int n;
        do {
            n = -1;
            System.out.print("姓名：");
            String name = new String();
            name = input.next();
            System.out.print("电话：");
            String telPhone = new String();
            telPhone = input.next();
            System.out.print("住址：");
            String address = new String();
            address = input.next();
            System.out.print("备注：");
            String note = new String();
            note = input.next();
            System.out.print("地址所在地：");
            String address_state = new String();
            address_state = input.next();
            ObjectOutputStream ois = null;

            try {

                ois = new ObjectOutputStream(new FileOutputStream("C:\\Users\\mac\\Desktop\\全部人员.doc"));
                list.add(new Water_Person(name, telPhone, address, note, address_state));
                ois.writeObject(list);
                System.out.println("人员信息添加成功！！！");

            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            } finally {
                if (ois != null) {
                    try {
                        ois.close();
                    } catch (IOException e) {
                        // TODO 自动生成的 catch 块
                        e.printStackTrace();
                    }
                }
            }
            System.out.println("是否继续添加下一条信息：（y/n）");
            char ch=input.next().charAt(0);
            if(ch=='y'||ch=='Y')
            {
                n=0;
            }
        }while(n==0);
    }

    public static void Del_Person()   //删除人员信息
    {
        if(list.size()==0)
        {
            System.out.println("人员信息为空！！！");
        }
        {

            System.out.print("请输入您要删除的人员姓名或者电话：");
            String name=new String();
            name=input.next();
            for(Water_Person wp:list)
            {
                if(name.equals(wp.getName())||(name.equals(wp.getTelPhone())))
                {
                    int index=list.indexOf(wp);
                    System.out.println("人员信息：\n姓名："+wp.getName()+"\n电话："+wp.getTelPhone()+"\n家庭住址："+wp.getAddress()+"\n备注："
                            +wp.getNote()+"\n地址所在地"+wp.getAddress_state());
                    System.out.println("确定删除吗？（Y/N）");
                    char ch=input.next().charAt(0);
                    if(ch=='Y'||ch=='y')
                    {
                        list.remove(index);
                        System.out.println("删除成功！！！");
                        ObjectOutputStream ois=null;

                        try {

                            ois=new ObjectOutputStream(new FileOutputStream("C:\\Users\\mac\\Desktop\\全部人员.doc"));
                            ois.writeObject(list);

                        } catch (IOException e) {
                            // TODO 自动生成的 catch 块
                            e.printStackTrace();
                        }finally
                        {
                            if(ois!=null)
                            {
                                try {
                                    ois.close();
                                } catch (IOException e) {
                                    // TODO 自动生成的 catch 块
                                    e.printStackTrace();
                                }
                            }
                        }
                        break;
                    }
                }
            }
        }
    }

    public static void Find_Person()     //查找指定人员信息
    {
        System.out.print("请输入您要查询的人员姓名或者电话：");
        if(list.size()==0)
        {
            System.out.println("人员信息为空！！！");
        }
        else{
            System.out.print("请输入您要查询的人员姓名或者电话：");
            String name=new String();
            name=input.next();
            int index=-1;
            for(Water_Person wp:list)
            {
                if(name.equals(wp.getName())||(name.equals(wp.getTelPhone())))
                {
                    index=list.indexOf(wp);
                    System.out.println("人员信息：\n姓名："+wp.getName()+"\n电话："+wp.getTelPhone()+"\n家庭住址："+wp.getAddress()+"\n备注："
                            +wp.getNote()+"\n地址所在地："+wp.getAddress_state());

                }
            }
            if(index==-1)
            {
                System.out.println("对不起，您要查找的人员信息不存在！！！");
            }
        }
    }

    public static void Amend_Person()   //修改指定人员信息
    {
        System.out.print("请输入您要修改的人员的姓名后者电话号码：");
        String telPhone=new String();
        telPhone=input.next();
        for(Water_Person wp:list)
        {
            if(telPhone.equals(wp.getName())||(telPhone.equals(wp.getTelPhone())))
            {
                int index=list.indexOf(wp);
                System.out.println("人员信息：\n姓名："+wp.getName()+"\n电话："+wp.getTelPhone()+"\n家庭住址："+wp.getAddress()+"\n备注："
                        +wp.getNote()+"\n地址所在地:"+wp.getAddress_state());
                int n=-1;
                do
                {
                    n=-1;
                    System.out.println("您要修改该名成员的哪项属性值：（1，姓名  2，电话  3，家庭住址  4，备注 5,地址所在地）");
                    System.out.print("您的选择是：");
                    int a=input.nextInt();
                    switch(a)
                    {

                        case 1:
                            System.out.print("姓名修改为：");
                            String name1=input.next();
                            list.get(index).setName(name1);
                            System.out.println("姓名修改成功！！");
                            break;
                        case 2:
                            System.out.print("电话号码修改为：");
                            String tel=input.next();
                            list.get(index).setTelPhone(tel);
                            System.out.println("电话号码修改成功！！");
                            break;
                        case 3:
                            System.out.print("家庭住址修改为：");
                            String address=input.next();
                            list.get(index).setAddress(address);
                            System.out.println("家庭住址修改成功！！");
                            break;
                        case 4:
                            System.out.print("备注修改为：");
                            String note=input.next();
                            list.get(index).setNote(note);
                            System.out.println("备注修改成功！！");
                            break;
                        case 5:
                            System.out.print("地址所在地修改为：");
                            String address_state=input.next();
                            list.get(index).setAddress_state(address_state);
                            System.out.println("地址所在地修改成功！！");
                            break;
                        default:
                            System.out.println("您的输入有误！！！");
                            break;
                    }
                    System.out.println("继续修改该名成员的其他属性值吗？（Y/N）");
                    char ch=input.next().charAt(0);
                    if(ch=='y'||ch=='Y')
                    {
                        n=0;
                    }

                }while(n==0);

                ObjectOutputStream ois=null;

                try {

                    ois=new ObjectOutputStream(new FileOutputStream("C:\\Users\\mac\\Desktop\\全部人员.doc"));
                    ois.writeObject(list);

                } catch (IOException e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }finally
                {
                    if(ois!=null)
                    {
                        try {
                            ois.close();
                        } catch (IOException e) {
                            // TODO 自动生成的 catch 块
                            e.printStackTrace();
                        }
                    }
                }
                break;
            }
        }


    }

    public static void Area_Person()     //查找同区域全部人员信息
    {
        System.out.println("请输入您需要查找的区域：");
        String address_state=input.next();

        File file=new File("C:\\Users\\mac\\Desktop\\"+address_state+"所有人员信息.doc");
        if(!file.exists())
        {
            try {
                file.createNewFile();
                System.out.println(address_state+"所有人员信息.doc文件加载成功！！！");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else
        {
            System.out.println(address_state+"所有人员信息.doc文件加载成功！！！");
        }
        try {

            PrintStream out = System.out;
            PrintStream ps = new PrintStream(new FileOutputStream("C:\\Users\\mac\\Desktop\\" + address_state + "所有人员信息.doc"));
            System.setOut(ps);
            int i=0;
            System.out.println("姓名\t\t电话\t\t\t家庭住址\t\t\t\t备注\t\t\t\t\t地址所在地");
            for(Water_Person wp:list)
            {
                if(wp.getAddress_state().equals(address_state))
                {
                    i++;

                    System.out.println(wp.getName()+"\t"+wp.getTelPhone()+"\t"+wp.getAddress()+"\t\t"+wp.getNote()+"\t\t"+wp.getAddress_state());
                }
            }
                ps.close();

            if(i==0)
            {
                System.setOut(out);
                File file1=new File("C:\\Users\\mac\\Desktop\\"+address_state+"所有人员信息.doc");
                file.deleteOnExit();
                file.delete();
                System.out.println("该区域内没有人员信息，自动删除空文件！！！");

            }
            else
            {
                System.setOut(out);
                System.out.println("人员信息已全部加载至桌面："+address_state+"所有人员信息.doc");

            }

        }catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }


    }

    public static void main(String args[])
    {

        int n=0;
        boolean isExit;

        initData();
        do
        {
            start();
            n=0;
            isExit=false;
            System.out.print("请选择：");
            int choose=1;
            try {
                choose=input.nextInt();
            }catch(Exception e)
            {
                System.err.println("输入错误，请输入相关数字\n请重新启动！！");
                System.exit(1);

            }
            switch(choose)
            {

                case 1:
                    System.out.println("***查看全部人员信息***");
                    To_View();
                    break;

                case 2:
                    System.out.println("***添加人员信息***");
                    add_Person();
                    break;

                case 3:
                    System.out.println("***删除人员信息***");
                    Del_Person();
                    break;

                case 4:
                    System.out.println("***查看相关人员信息***");
                    Find_Person();
                    break;

                case 5:
                    System.out.println("***修改相关人员信息***");
                    Amend_Person();
                    break;

                case 6:
                    System.out.println("***查找区域类人员信息***");
                    Area_Person();
                    break;
                case 7:
                    System.out.println("***退出系统***");
                    isExit=true;
                    break;

            }
            if(isExit==false)
            {
                System.out.println("输入0返回主菜单");
                try
                {
                    n=input.nextInt();
                }catch(Exception e)
                {
                    System.err.println("输入错误，请输入相关数字\n请重新启动！！");
                    System.exit(1);
                }

            }
            else
            {
                System.out.println("谢谢使用！！！");
                System.exit(1);
            }

        }while(n==0);


    }

}
